import ast
import jsonpath

# 从excel中读取断言
check_str = '[{"expr":"$.code","expected": 200,"type":"eq"},{"expr":"$.msg","expected":"登录成功","type":"eq"}]'

# 把字符串转换成python列表
check_list = ast.literal_eval(check_str)
print(check_list)

# 响应结果
response = {"code": 200, "msg": "登录成功"}

check_res = []
for check in check_list:
    # 通过josnpath表达式，从响应结果中拿到实际结果
    actual = jsonpath.jsonpath(response, check["expr"])
    if isinstance(actual,list):
        actual = actual[0]
    # 与实际结果做比对
    if check["type"] == "eq":
        check_res.append(actual == check["expected"])

if False in check_res:
    AssertionError
